<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>DES加密解密</title>
</head>

<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
  <script>

    // 定义加密函数
    function encryptByDES(message, key) {
      var keyHex = CryptoJS.enc.Utf8.parse(key);
      var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      });
      return encrypted.toString();
    }

    // 定义解密函数
    function decryptByDES(ciphertext, key) {
      var keyHex = CryptoJS.enc.Utf8.parse(key);
      var decrypted = CryptoJS.DES.decrypt(ciphertext, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      });
      return decrypted.toString(CryptoJS.enc.Utf8);
    }

    // 加密解密使用
    var message = "Hello, world!";
    var key = "12345678";

    // 输出加密的内容
    var encrypted = encryptByDES(message, key);
    console.log('输出加密的内容' + encrypted);

    // 输出解密的内容
    var decrypted = decryptByDES(encrypted, key);
    console.log('输出解密的内容' + decrypted);

    // -----------------------
    // JSR223 加密
    var xiphweType = 'SM2'; // 定义加密类型变量 
    load("sm2-1.3.1.min.js"); // 加载SM2加密库 
    var cipher = new SM2CipherUtil(xiphweType); // 创建SM2加密对象 
    var ciphertext = cipher.sm2Encrypt("1qaz@WSX"); // 加密明文密码 
    vars.put("pwd", ciphertext); // 存储加密后的密码到JMeter变量pwd中
    vars.get("pwd"); // 获取已经加密的密码
  </script>
</body>

</html>